package com.ruoyi.web.controller.fs;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.fs.domain.FsCase;
import com.ruoyi.fs.domain.FsCaseRepaymentSchedule;
import com.ruoyi.fs.domain.FsRepaymentStatistic;
import com.ruoyi.fs.service.IFsCaseRepaymentScheduleService;
import com.ruoyi.fs.service.IFsCaseService;
import com.ruoyi.fs.service.IFsRepaymentStatisticService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fs.domain.FsCaseRepayment;
import com.ruoyi.fs.service.IFsCaseRepaymentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 案件还款Controller
 *
 * @author hero
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/fs/repayment")
public class FsCaseRepaymentController extends BaseController {
    @Autowired
    private IFsCaseRepaymentService fsCaseRepaymentService;
    @Autowired
    private IFsCaseRepaymentScheduleService fsCaseRepaymentScheduleService;
    @Autowired
    private IFsRepaymentStatisticService fsRepaymentStatisticService;
    @Autowired
    private IFsCaseService fsCaseService;

    /**
     * 查询案件还款列表
     */
    @PreAuthorize("@ss.hasPermi('fs:case:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsCaseRepayment fsCaseRepayment) {
        startPage();
        List<FsCaseRepayment> list = fsCaseRepaymentService.selectFsCaseRepaymentList(fsCaseRepayment);
        return getDataTable(list);
    }

    /**
     * 导出案件还款列表
     */
    @PreAuthorize("@ss.hasPermi('fs:case:export')")
    @Log(title = "案件还款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsCaseRepayment fsCaseRepayment) {
        List<FsCaseRepayment> list = fsCaseRepaymentService.selectFsCaseRepaymentList(fsCaseRepayment);
        ExcelUtil<FsCaseRepayment> util = new ExcelUtil<FsCaseRepayment>(FsCaseRepayment.class);
        util.exportExcel(response, list, "案件还款数据");
    }

    /**
     * 获取案件还款详细信息
     */
    @PreAuthorize("@ss.hasPermi('fs:case:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fsCaseRepaymentService.selectFsCaseRepaymentById(id));
    }

    /**
     * 新增案件还款
     */
    @PreAuthorize("@ss.hasPermi('fs:case:add')")
    @Log(title = "案件还款", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsCaseRepayment fsCaseRepayment) {
        LoginUser loginUser = getLoginUser();
        fsCaseRepayment.setCreateBy(loginUser.getUser().getNickName());
        FsCase fsCase = fsCaseService.selectFsCaseById(fsCaseRepayment.getCaseId());
        fsCaseRepayment.setSsr(fsCase.getTjyId());
        fsCaseRepayment.setGxsj(new Date());
        fsCaseRepaymentService.insertFsCaseRepayment(fsCaseRepayment);
        List<FsCaseRepaymentSchedule> hkjhList = fsCaseRepayment.getHkjhList();
        for (int i = 0; i < hkjhList.size(); i++) {
            FsCaseRepaymentSchedule schedule = hkjhList.get(i);
            schedule.setRepaymentId(fsCaseRepayment.getId());
            schedule.setCaseId(fsCase.getId());
            schedule.setZt(0);
            schedule.setSsr(fsCase.getTjyId());
            schedule.setGxsj(new Date());
            schedule.setCreateBy(loginUser.getUser().getNickName());
            fsCaseRepaymentScheduleService.insertFsCaseRepaymentSchedule(schedule);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(schedule.getJhrq());
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1;
            FsRepaymentStatistic statistic = new FsRepaymentStatistic();
            statistic.setSsr(fsCase.getTjyId());
            statistic.setYear(year);
            statistic.setMonth(month);
            List<FsRepaymentStatistic> statistics = fsRepaymentStatisticService.selectFsRepaymentStatisticList(statistic);
            int znum = 0;
            BigDecimal zje = BigDecimal.ZERO;
            int dhnum = 0;
            BigDecimal dhje = BigDecimal.ZERO;
            if (!statistics.isEmpty()) {
                statistic = statistics.get(0);
                znum = statistic.getZnum();
                zje = statistic.getZje();
                dhnum = statistic.getDhnum();
                dhje = statistic.getDhje();
            }
            znum++;
            dhnum++;
            zje = zje.add(schedule.getJhje());
            dhje = dhje.add(schedule.getJhje());
            statistic.setZnum(znum);
            statistic.setZje(zje);
            statistic.setDhnum(dhnum);
            statistic.setDhje(dhje);
            if (statistic.getId() != null) {
                fsRepaymentStatisticService.updateFsRepaymentStatistic(statistic);
            } else {
                fsRepaymentStatisticService.insertFsRepaymentStatistic(statistic);
            }
        }
        fsCase.setIfRepayment(1);
        fsCaseService.updateFsCase(fsCase);
        return success();
    }

    /**
     * 修改案件还款
     */
    @PreAuthorize("@ss.hasPermi('fs:case:edit')")
    @Log(title = "案件还款", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsCaseRepayment fsCaseRepayment) {
        return toAjax(fsCaseRepaymentService.updateFsCaseRepayment(fsCaseRepayment));
    }

    /**
     * 删除案件还款
     */
    @PreAuthorize("@ss.hasPermi('fs:case:remove')")
    @Log(title = "案件还款", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fsCaseRepaymentService.deleteFsCaseRepaymentByIds(ids));
    }
}
